﻿using Application.MainModule.OrderDetailManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Domain.MainModule.OrderDetails;
using Domain.MainModule.Entities;
using System.Collections.Generic;
using Infraestructure.CrossCutting.IoC;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for OrderDetailManagementServiceTest and is intended
    ///to contain all OrderDetailManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class OrderDetailManagementServiceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for AddOrderDetail
        ///</summary>
        [TestMethod()]
        public void AddOrderDetail_Invoke_Test()
        {
            //Arrange
            IOrderDetailManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderDetailManagementService>();
            Guid orderDetailId = Guid.NewGuid();
            OrderDetail orderDetail = new OrderDetail()
            {
                OrderDetailId = orderDetailId
            };

            //Act
            target.AddOrderDetail(orderDetail);
            OrderDetail result = target.FindOrderDetailById(orderDetailId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(orderDetailId, result.OrderDetailId);
            target.RemoveOrderDetail(result);
        }

        /// <summary>
        ///A test for ChangeOrderDetail
        ///</summary>
        [TestMethod()]
        public void ChangeOrderDetail_Invoke_Test()
        {
            //Arrange
            IOrderDetailManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderDetailManagementService>();
            Guid orderDetailId = Guid.Parse("ABCDF456-1234-AACC-4444-AAAACCBBBBBB");
            OrderDetail orderDetail = null;

            //Act
            orderDetail = target.FindOrderDetailById(orderDetailId);
            Guid oldValue = orderDetail.OrderId;
            orderDetail.OrderId = Guid.NewGuid();
            target.ChangeOrderDetail(orderDetail);

            //Assert
            OrderDetail result = target.FindOrderDetailById(orderDetailId);
            Assert.IsNotNull(result);
            Assert.AreNotEqual(oldValue, result.OrderId);
        }

		/// <summary>
        ///A test for FindOrderDetailById
        ///</summary>
        [TestMethod()]
        public void FindOrderDetailById_Invoke_Test()
        {
            //Arrange
            IOrderDetailManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderDetailManagementService>();
            Guid orderDetailId = Guid.Parse("ABCDF456-1234-AACC-4444-AAAACCBBBBBB");

            //Act 
            OrderDetail orderDetail = target.FindOrderDetailById(orderDetailId);

            //Assert
            Assert.IsNotNull(orderDetail);
            Assert.AreEqual(orderDetailId, orderDetail.OrderDetailId);
        }
		
        /// <summary>
        ///A test for FindOrderDetailByOrderId
        ///</summary>
        [TestMethod()]
        public void FindOrderDetailByOrderId_Invoke_Test()
        {
            //Arrange
            IOrderDetailManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderDetailManagementService>();
            Guid orderId = Guid.Parse("ABCDF456-1234-AABB-3333-AAAACCBBB123");

            //Act
            List<OrderDetail> actual;
            actual = target.FindOrderDetailByOrderId(orderId);

            //Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual(orderId, actual[0].OrderId);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveOrderDetail_Invoke_NullItemThrowNewArgumentNullException_Test()
        {
            //Arrange
            IOrderDetailManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderDetailManagementService>();

            //Act
            target.RemoveOrderDetail(null);
        }

        /// <summary>
        ///A test for RemoveOrderDetail
        ///</summary>
        [TestMethod()]
        public void RemoveOrderDetail_Invoke_Test()
        {
            //Arrange
            IOrderDetailManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IOrderDetailManagementService>();
            OrderDetail orderDetail = null;

            //Act
            orderDetail = target.FindOrderDetailById(Guid.Parse("ABCDF456-1234-AABB-3333-AAAACCBBBBBB"));
            target.RemoveOrderDetail(orderDetail);

            //Assert
            OrderDetail result = target.FindOrderDetailById(Guid.Parse("ABCDF456-1234-AABB-3333-AAAACCBBBBBB"));
            Assert.IsNull(result);
            orderDetail = new OrderDetail()
                    {
                        OrderDetailId = Guid.Parse("ABCDF456-1234-AABB-3333-AAAACCBBBBBB"),
                        Order = new Order()
                        {
                            ExtraInfo = "extra info 01"
                        },
                        OrderId = Guid.Parse("ABCDF456-1234-AABB-3333-AAAACCBBB123")
                    };
            target.AddOrderDetail(orderDetail);
        }
    }
}
